package cn.lopr.im.android.mvp.presenter;

import java.util.Map;

import cn.lopr.im.android.bean.Article;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.PageInfo;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.ICommunityContact;
import cn.lopr.im.android.mvp.contact.IPersonalHomepageContact;
import cn.lopr.im.android.mvp.model.CommunityModel;
import cn.lopr.im.android.mvp.model.PersonalHomepageModel;

public class PersonalHomepagePresenterImpl implements IPersonalHomepageContact.IPersonalHomepagePresenter {
    private IPersonalHomepageContact.IPersonalHomepageModel personalHomepageModel;
    private IPersonalHomepageContact.IPersonalHomepageView personalHomepageView;
    private ICommunityContact.ICommunityModel communityModel;

    public PersonalHomepagePresenterImpl(IPersonalHomepageContact.IPersonalHomepageView personalHomepageView) {
        this.personalHomepageModel = new PersonalHomepageModel();
        this.personalHomepageView = personalHomepageView;
        this.communityModel = new CommunityModel();
    }

    @Override
    public void getUserInfoById(String token, int userId) {
        personalHomepageModel.getUserInfoById(token, userId, new OnCallBackListener<CallbackResult<User>, String>() {
            @Override
            public void success(CallbackResult<User> data) {
                personalHomepageView.getUserInfoSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {
                personalHomepageView.showVerifyFailed(errMsg);
            }
        });
    }

    @Override
    public void getArticle(String token, Integer userId, int page, int size) {
        communityModel.getArticleListByUserId(token, userId, page, size, new OnCallBackListener<CallbackResult<PageInfo<ArticleVo>>, String>() {
            @Override
            public void success(CallbackResult<PageInfo<ArticleVo>> data) {
                personalHomepageView.getArticleSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {

            }
        });
    }

    @Override
    public void getLikeMeLikeYouVisitPublish(Integer userId) {

        communityModel.getLikeMeLikeYouVisitPublish(userId, new OnCallBackListener<CallbackResult<Map<String, Integer>>, String>() {
            @Override
            public void success(CallbackResult<Map<String, Integer>> data) {
                personalHomepageView.getLikeMeLikeYouVisitPublish(data.getData());
            }

            @Override
            public void failure(String errMsg) {

            }
        });
    }

    @Override
    public void updateArticleLikeNum(String token, String articleId, int num) {
        Article article = new Article().setId(articleId).setLike(num);

        this.communityModel.updateArticle(token, article, new OnCallBackListener<CallbackResult<String>, String>() {
            @Override
            public void success(CallbackResult<String> data) {
                personalHomepageView.updateArticleSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {
                personalHomepageView.showVerifyFailed(errMsg);
            }
        });
    }
}
